package com.lcx.nfctest.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.lcx.nfctest.nfctestinfo.NfcTestInfo;

import java.util.ArrayList;
import java.util.List;


public class UserDBHelper extends SQLiteOpenHelper {
    private static final String name = "NfcTest.db";
    private static final String table_name = "NfcTestInfo";
    private static final int DbVersion = 1;
    private static UserDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;
    private String TAG = "lcx_debug";

    public UserDBHelper(@Nullable Context context) {
        super(context, name, null, DbVersion);
    }
    public static UserDBHelper getInstance(Context context){

        if (mHelper == null){
            mHelper = new UserDBHelper(context);
        }
        return mHelper;
    }
    //连接读数据库
    public SQLiteDatabase openReadLink(){
        if (mRDB == null || !mRDB.isOpen()){
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    //连接写数据库
    public SQLiteDatabase openWriteLink(){
        if (mWDB == null || !mWDB.isOpen()){
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public void closeLink(){
        if (mRDB != null && mRDB.isOpen()){
            mRDB.close();
            mRDB = null;
        }
        if (mWDB != null && mWDB.isOpen()){
            mWDB.close();
            mWDB = null;
        }
    }
    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    //创建数据库表
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "onCreate:SQLiteDatabase ");
        String sql = "create table IF NOT EXISTS " + table_name + "(num INTEGER PRIMARY KEY AUTOINCREMENT,time integer,tagId varchar,readCardStartTime varchar,readCardEndTime varchar)";
        db.execSQL(sql);
        //db.execSQL("insert into NfcTestInfo (readTime,time) values ('1','1')");

    }

    /**
     * Called when the database needs to be upgraded. The implementation
     * should use this method to drop tables, add tables, or do anything else it
     * needs to upgrade to the new schema version.
     *
     * <p>
     * The SQLite ALTER TABLE documentation can be found
     * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
     * you can use ALTER TABLE to rename the old table, then create the new table and then
     * populate the new table with the contents of the old table.
     * </p><p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     * <p>
     * <em>Important:</em> You should NOT modify an existing migration step from version X to X+1
     * once a build has been released containing that migration step.  If a migration step has an
     * error and it runs on a device, the step will NOT re-run itself in the future if a fix is made
     * to the migration step.</p>
     * <p>For example, suppose a migration step renames a database column from {@code foo} to
     * {@code bar} when the name should have been {@code baz}.  If that migration step is released
     * in a build and runs on a user's device, the column will be renamed to {@code bar}.  If the
     * developer subsequently edits this same migration step to change the name to {@code baz} as
     * intended, the user devices which have already run this step will still have the name
     * {@code bar}.  Instead, a NEW migration step should be created to correct the error and rename
     * {@code bar} to {@code baz}, ensuring the error is corrected on devices which have already run
     * the migration step with the error.</p>
     *
     * @param db         The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }
    public long insert(NfcTestInfo nfcTestInfo){
        ContentValues values = new ContentValues();
        values.put("time",nfcTestInfo.getTime());
        values.put("tagId",nfcTestInfo.getTagId());
        values.put("readCardStartTime",nfcTestInfo.getReadCardStartTime());
        values.put("readCardEndTime",nfcTestInfo.getReadCardEndTime());
        Log.d(TAG, "insert: " + values.toString());
        return mWDB.insert(table_name,null,values);

    }
    public long deleteAllRecord(){
        //return mWDB.delete(table_name,"num=?",new String [] {num});
        return mWDB.delete(table_name,"1=1",null);
    }

    public List<NfcTestInfo> queryAllRecord(){
        //return mWDB.delete(table_name,"num=?",new String [] {num});
        List<NfcTestInfo> list = new ArrayList<NfcTestInfo>();
        Cursor cursor = mWDB.query(table_name,null,null,null,null,null,null);

        while (cursor.moveToNext()){
            NfcTestInfo nfcTestInfo = new NfcTestInfo();
            nfcTestInfo.setNum(cursor.getInt(0));
            nfcTestInfo.setTime(cursor.getInt(1));
            nfcTestInfo.setTagId(cursor.getString(2));
            nfcTestInfo.setReadCardStartTime(cursor.getString(3));
            nfcTestInfo.setReadCardEndTime(cursor.getString(4));
            list.add(nfcTestInfo);
        }
        return list;
    }
}
